<!--
 * @Descripttion: 
 * @version: 
 * @Author: sueRimn
 * @Date: 2022-08-19 11:30:52
 * @LastEditors: sueRimn
 * @LastEditTime: 2023-02-23 15:30:13
-->
<template>
  <div class="component_container">
    <!-- 查询表单 -->
    <div>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="">
          <el-input
            v-model="formInline.code"
            placeholder="请输入数据视图编码"
          ></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-input
            v-model="formInline.description"
            placeholder="请输入数据视图描述"
          ></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-select
            v-model="formInline.component_code"
            placeholder="请选择对应组件编码"
            style="float: left"
          >
            <el-option
              v-for="item in componentList"
              :label="item.label"
              :value="item.code"
              :key="item.code"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="">
          <el-input
            v-model="formInline.content"
            placeholder="请输入数据视图内容"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addDataView">添加</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 查询结果列表显示 -->
    <el-table :data="tableData" style="width: 100%" ref="dataViewTalbe">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" class="demo-table-expand">
            <el-form-item label="数据视图ID">
              <span>{{ props.row.id }}</span>
            </el-form-item>
            <el-form-item label="数据视图描述">
              <span v-if="!props.row.edit">{{ props.row.description }}</span>
              <el-input v-else v-model="props.row.description"></el-input>
            </el-form-item>
            <el-form-item label="数据视图排序值">
              <span v-if="!props.row.edit">{{ props.row.sort_number }}</span>
              <el-input v-else v-model="props.row.sort_number"></el-input>
            </el-form-item>
            <el-form-item label="最后修改时间">
              <span>{{ props.row.last_modified_time }}</span>
            </el-form-item>
            <el-form-item label="数据视图编码">
              <span v-if="!props.row.edit">{{ props.row.code }}</span>
              <el-input v-else v-model="props.row.code"></el-input>
            </el-form-item>
            <el-form-item label="对应组件编码">
              <span v-if="!props.row.edit">{{ props.row.component_code }}</span>
              <el-select
                v-else
                placeholder="请选择对应组件"
                style="float: left"
                v-model="props.row.component_code"
              >
                <el-option
                  v-for="item in componentList"
                  :label="item.label"
                  :value="item.code"
                  :key="item.code"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="最后修改人">
              <span>{{ props.row.last_modified_author }}</span>
            </el-form-item>
            <el-form-item label="数据视图内容" style="width: 100%">
              <span v-if="!props.row.edit">{{ props.row.content }}</span>
              <el-input
                type="textarea"
                v-else
                v-model="props.row.content"
                :autosize="{ minRows: 2 }"
                class="contentArea"
              ></el-input>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="数据视图编码" prop="code"> </el-table-column>
      <el-table-column label="数据视图描述" prop="description">
      </el-table-column>
      <el-table-column label="对应组件编码" prop="component_code">
      </el-table-column>
      <el-table-column label="操作" prop="operate">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.edit"
            type="default"
            @click="saveEditDataView(scope.row)"
            >保存</el-button
          >
          <el-button
            v-if="scope.row.edit"
            type="default"
            @click="cancelEditDataView(scope.row)"
            >取消</el-button
          >
          <el-button
            v-if="!scope.row.edit"
            type="primary"
            @click="editDataView(scope.row)"
            >编辑</el-button
          >
          <el-button type="primary" @click="delDataView(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 新增数据视图弹出框 -->
    <el-dialog
      title="新增数据视图"
      :visible.sync="addDataViewDialogVisible"
      style="height: 650px"
    >
      <el-form
        :model="addDataViewForm"
        label-position="left"
        label-width="120px"
        class="demo-ruleForm"
        status-icon
        :rules="addRules"
        ref="addForm"
        size="medium"
      >
        <el-form-item label="视图编码">
          <el-input
            v-model="addDataViewForm.code"
            placeholder="请输入数据视图编码"
          ></el-input>
        </el-form-item>
        <el-form-item label="数据视图描述">
          <el-input
            v-model="addDataViewForm.description"
            placeholder="请输入数据视图描述"
          ></el-input>
        </el-form-item>
        <el-form-item label="对应组件编码">
          <el-select
            v-model="addDataViewForm.component_code"
            placeholder="请选择对应组件编码"
            style="float: left"
          >
            <el-option
              v-for="item in componentList"
              :label="item.label"
              :value="item.code"
              :key="item.code"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数据视图排序">
          <el-input
            v-model="addDataViewForm.sort_number"
            placeholder="请输入数据视图排序"
          ></el-input>
        </el-form-item>
        <el-form-item label="数据视图内容">
          <el-input
            v-model="addDataViewForm.content"
            placeholder="请输入数据视图内容"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDataViewDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmAdd('addForm')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "dataView",
  data() {
    return {
      addDataViewDialogVisible: false,
      formInline: {
        code: "",
        description: "",
        component_code: "",
        content: "",
      },
      addDataViewForm: {
        code: "",
        description: "",
        component_code: "",
        sort_number: "",
        content: "",
      },
      tableData: [],
      addRules: {},
      componentList: [],
    };
  },
  created() {
    this.getDataViewList();
  },
  mounted() {
    this.$nextTick(() => {
      this.getComponentList();
    });
  },
  methods: {
    addDataView() {
      this.addDataViewDialogVisible = true;
    },
    getComponentList() {
      this.$http.component
        .getList(this.$address.baseUrl, {})
        .then((res) => {
          if (res.status) {
            this.componentList = [];
            res.data.forEach((item) => {
              let tmp = {
                code: item.code,
                label: item.description + "(" + item.code + ")",
              };
              this.componentList.push(tmp);
            });
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    getDataViewList() {
      this.$http.dataView
        .getList(this.$address.baseUrl, { ...this.formInline })
        .then((res) => {
          if (res.status) {
            this.tableData = res.data;
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    confirmAdd(formName) {
      this.addDataViewDialogVisible = false;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let params = {
            create_author: this.$store.state.user.userInfo.login_name,
            ...this.addDataViewForm,
          };
          this.$http.dataView
            .add(this.$address.baseUrl, params)
            .then((res) => {
              if (res.status) {
                this.getDataViewList();
              }
            })
            .catch((error) => {
              console.log(error);
            });
        }
      });
    },
    search() {
      this.getDataViewList();
    },
    saveEditDataView(row) {
      let params = {
        ...row,
      };
      params.content = params.content
        .toString()
        .replace(new RegExp('\\\\"', "gm"), '"')
        .replace(/\r\n/g, "")
        .replace(new RegExp('\\n"', "gm"), "");
      this.$delete(params, "edit");
      params.last_modified_author = this.$store.state.user.userInfo.login_name;
      this.$http.dataView
        .edit(this.$address.baseUrl, params)
        .then((res) => {
          if (res.status) {
            this.getDataViewList();
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    cancelEditDataView(row) {
      this.$set(row, "edit", false);
      this.$nextTick(() => {
        this.$refs.dataViewTalbe.toggleRowExpansion(row, false);
      });
    },
    editDataView(row) {
      this.$set(row, "edit", true);
      this.$nextTick(() => {
        this.$refs.dataViewTalbe.toggleRowExpansion(row, true);
      });
    },
    delDataView(row) {
      this.$http.dataView
        .delete(this.$address.baseUrl, { id: row.id })
        .then((res) => {
          if (res.status) {
            this.getDataViewList();
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
  },
};
</script>
<style scoped>
.el-form-item--small.el-form-item {
  float: left;
}
.demo-table-expand div {
  width: 45%;
  margin-left: 40px;
  margin-bottom: 0px;
}
.demo-table-expand div .contentArea {
  width: calc(100% - 200px);
}
</style>